package xianchenglock1;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.LockSupport;

/**
 * @功能
 * @创建时间 2019/12/18
 * 这里使用wait和notify识别，wait会释放锁，notify不会释放锁
 * 需要注意的是，运用这种方法，必须保证t2先执行，也就是首先让t2监听才可以
 *
 * 可以读到输出结果并不是size=5时t2退出，而是t3结束时t2才收到通知而退出。。。？
 */
public class MyContainer5 {
    //添加v。。。 使t2能得到通知
    volatile List lists = new ArrayList();
    public void add(Object o){
        lists.add(o);
    }
    public int size(){
        return lists.size();
    }
    public final static Integer a=0;
    public static CountDownLatch latch = new CountDownLatch(1);
    static char[] b = "123456".toCharArray();
    static char[] d = "ABCDEF".toCharArray();
    static Thread t2;
    static Thread t1;
    public static void main(String[] args) {
        final Object o = new Object();
      t1=  new Thread(()->{

          synchronized (o){
//              latch.countDown();
               for(char i:b){

//                   LockSupport.unpark(t2);
//                   LockSupport.park();

                       System.out.println(i);
                   try {
                       o.notify();
                       o.wait();

                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }

                   }
                     o.notify();


           }
       });
        t2 = new Thread(()->{
//            try {
//                latch.await();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            try {
//                Thread.sleep(2000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            synchronized (o){
                for(char i:d){

                    System.out.println(i);
                    try {
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }


            }
o.notify();}
        },"t2");
       t2.start();
       t1.start();
    }
    }



